<?php
namespace App\Service\Admin;

use Illuminate\Support\Facades\DB;

class DeviceService extends BaseService
{
    //设备商城

    protected $pasTypes = [
        '', '屏幕尺寸', '系统配置', '接口类型', '触摸屏类型', '设备用途',
    ];

    /**
     * 列表
     * @param int $type 分类
     */
    public function getPasIndex($type)
    {
        $db = $this->dbConn($this->dbDevicePas)
            ->where('del', 0);
        if ($type) {
            $db = $db->where('type', $type);
        }
        $res = $db->get();
        if (!count($res)) {
            return $this->toback(-2, '没有记录');
        }
        $types = $this->pasTypes;
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            $type_name = array_key_exists($v['type'], $types) ? $types[$v['type']] : '-';
            $arys[] = [
                'id' => $v['id'],
                'type' => $v['type'],
                'type_name' => $type_name,
                'title' => $v['title'],
                'uptime' => $v['uptime'],
            ];
        }
        return $this->toback(0, '', $arys);
    }

    //参数添加
    public function pasAdd($pas)
    {
        $res = (array) $this->dbConn($this->dbDevicePas)
            ->where('del', 0)
            ->where('type', $pas['type'])
            ->where('title', $pas['title'])
            ->first();
        if (!empty($res)) {
            return $this->toback(-2, '存在同名参数');
        }
        $lastid = $this->dbConn($this->dbDevicePas)->insertGetId([
            'type' => $pas['type'], 'title' => $pas['title'],
            'addtime' => toDate(), 'uptime' => toDate(),
        ]);
        if (!$lastid) {
            return $this->toback(-1, '添加失败');
        }
        return $this->toback();
    }

    //参数修改
    public function pasModify($id, $pas)
    {
        $res = (array) $this->dbConn($this->dbDevicePas)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return $this->toback(-2, '参数不存在');
        }
        $up = $this->dbConn($this->dbDevicePas)->where('id', $id)
            ->update([
                'title' => $pas['title'], 'uptime' => toDate(),
            ]);
        if (!$up) {
            return $this->toback(-1, '修改失败');
        }
        return $this->toback();
    }

    //参数删除
    public function pasDel($id)
    {
        $res = (array) $this->dbConn($this->dbDevicePas)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return $this->toback(-2, '参数不存在');
        }
        $up = $this->dbConn($this->dbDevicePas)->where('id', $id)
            ->update([
                'del' => 1, 'deltime' => toDate(),
            ]);
        if (!$up) {
            return $this->toback(-1, '删除失败');
        }
        return $this->toback();
    }

    //选项
    public function getOption()
    {
        $arys = [
            'cate' => [], //设备分类
            'area' => [], //地区
            'size' => [], //设备尺寸
            'sys' => [], //系统配置
            'api' => [], //接口类型
            'touch' => [], //触摸屏类型
            'usage' => [], //设备用途
        ];
        $arys['cate'] = $this->dbConn($this->dbCate)
            ->select('id', 'title')
            ->where('del', 0)
            ->where('type', 4)
            ->get();
        $arys['area'] = $this->areaOption();
        $arys['size'] = $this->dbConn($this->dbDevicePas)
            ->select('id', 'title')
            ->where('type', 1)
            ->where('del', 0)
            ->get();
        $arys['sys'] = $this->dbConn($this->dbDevicePas)
            ->select('id', 'title')
            ->where('del', 0)
            ->where('type', 2)
            ->get();
        $arys['api'] = $this->dbConn($this->dbDevicePas)
            ->select('id', 'title')
            ->where('del', 0)
            ->where('type', 3)
            ->get();
        $arys['touch'] = $this->dbConn($this->dbDevicePas)
            ->select('id', 'title')
            ->where('del', 0)
            ->where('type', 4)
            ->get();
        $arys['usage'] = $this->dbConn($this->dbDevicePas)
            ->select('id', 'title')
            ->where('del', 0)
            ->where('type', 5)
            ->get();
        return $arys;
    }

    /**
     * 列表
     * @param int $cateid 分类ID
     * @param array $pages【start，limit】
     */
    public function getIndex($pages)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = $this->dbConn($this->dbDevice)->where('del', 0);
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])->get();
        if (!count($res)) {
            return $this->toback(-2, '没有记录');
        }
        $arys = [];
        foreach ($res as $k => $v) {
            $v = (array) $v;
            //企业信息
            $comOne = (array) $this->dbConn($this->dbCompany)->where('id', $v['cid'])->first();
            $cname = empty($comOne) ? '-' : $comOne['cname'];
            //分类信息
            $cateOne = (array) $this->dbConn($this->dbCate)->where('id', $v['cate_id'])->first();
            $cateName = empty($cateOne) ? '-' : $cateOne['title'];
            $thumb = file_exists($v['thumb']) ? env('DOMAIN').'/'.$v['thumb'] : '';
            $arys[] = [
                'id' => $v['id'],
                'com_id' => $v['cid'],
                'com_name' => $cname,
                'title' => $v['title'],
                'cate_id' => $v['cate_id'],
                'cate_name' => $cateName,
                'thumb' => $thumb,
                'price' => $v['price'] * 100,
                'num' => $v['num'],
                'sale' => $v['sale'],
            ];
        }
        return $this->toback(0, '', $arys, $total);
    }

//    //一个
//    public function getOne($id)
//    {
//        $res = (array) $this->dbConn($this->dbCase)
//            ->where('del', 0)
//            ->where('id', $id)
//            ->first();
//        if (empty($res)) {
//            return $this->toback(-2, '没有记录');
//        }
//        //企业信息
//        $comOne = (array) $this->dbConn($this->dbCompany)->where('id', $res['cid'])->first();
//        $cname = empty($comOne) ? '-' : $comOne['cname'];
//        //$clogo = !empty($comOne)&&file_exists($comOne['logo']) ? env('DOMAIN').'/'.$comOne['logo'] : '';
//        //分类信息
//        $cateOne = (array) $this->dbConn($this->dbCate)->where('id', $res['cate_id'])->first();
//        $cateName = empty($cateOne) ? '-' : $cateOne['title'];
//        $thumb = file_exists($res['thumb']) ? env('DOMAIN').'/'.$res['thumb'] : '';
//        $arys = [
//            'title' => $res['title'],
//            'com_id' => $res['cid'],
//            'com_name' => $cname,
//            'cate_id' => $res['cate_id'],
//            'cate_name' => $cateName,
//            'address' => $res['address'],
//            'buyer_name' => $res['buyer_name'],
//            'designer_name' => $res['designer_name'],
//            'item_area' => $res['item_area'],
//            'main_material' => $res['main_material'] ? implode('，', explode('||||', $res['main_material'])) : '',
//            'thumb' => $thumb,
//            'item_finish_time' => $res['item_finish_time'],
//            'item_detail' => $res['item_detail'] ? htmlspecialchars_decode($res['item_detail']) : '',
//            'like_num' => $res['like_num'],
//            'collect_num' => $res['collect_num'],
//            'forward_num' => $res['forward_num'],
//        ];
//        return $this->toback(0, '', $arys);
//    }

//    //添加
//    public function add($type, $pas)
//    {
//        $arys = [
//            'type' => $type,
//            'json' => json_encode($pas, JSON_UNESCAPED_UNICODE),
//            'addtime' => toDate(),
//        ];
//        $lastid = $this->dbConn($this->dbYewu)->insertGetId($arys);
//        if (!$lastid) {
//            return $this->toback(-1, '添加失败');
//        }
//        return $this->toback();
//    }
}